﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class UserControl_UsersManager_thong_tin_ca_nhan : System.Web.UI.UserControl
{
    HttpCookie reqCookies = HttpContext.Current.Request.Cookies[basConst.COOKIE_USER_INFO];
    protected DataTable dtUser;
    protected string strAvarta;
    protected string strHoTen;
    private const string IMAGES_PATH = "/Images/AvartaUser/";
    private const int widthImages = 300;//Resize kích thước ảnh khi upload
    const string MSG_UPDATE_OK = "Cập nhật thông tin thành công !";
    //const string MSG_UPDATE_NOT_OK = "Cập nhật thông tin phát sinh lỗi vui lòng kiểm tra lại !";

    protected void Page_Load(object sender, EventArgs e)
    {
        xgetDetailUser();
    }

    private void xgetDetailUser()
    {
        txtLastLogin.Value = OsPortal.oStringHelper.ConvertDateTime_to_String(Convert.ToDateTime(reqCookies[basConst.COOKIE_USER_LASTLOGIN]),1);
        string strUser_ID = reqCookies[basConst.COOKIE_USER_ID].ToString();
        dtUser = clsUsers.USER_FND(1,1,strUser_ID,"","",-1,-1).Tables[0];
        txtEmail.Value = dtUser.Rows[0]["us_email"].ToString();
        txtHoTen.Value = dtUser.Rows[0]["us_hoten"].ToString();
        strHoTen = dtUser.Rows[0]["us_hoten"].ToString();
        txtDiaChi.Value = dtUser.Rows[0]["us_diachi"].ToString();
        txtDienThoai.Value = dtUser.Rows[0]["us_dienthoai"].ToString();
        string strLuotDang = dtUser.Rows[0]["us_luotdang"].ToString();
        if ((dtUser.Rows[0]["us_nhanmail"].ToString()).ToLower() == "true")
        {
            rbtTrue.Checked = true;
        }
        else
        {
            rbtFalse.Checked = true;
        }
        strAvarta = dtUser.Rows[0]["us_avatar"].ToString().Trim();
        if (string.IsNullOrEmpty(strAvarta)) { strAvarta = "/Images/System/no_image.gif"; }
        hdAvarta.Value = strAvarta;
    }

    private clsUsers.stUSER xgetvalue()
    {
        clsUsers.stUSER objUser = new clsUsers.stUSER();
        objUser.strUS_ID = reqCookies[basConst.COOKIE_USER_ID].ToString();
        objUser.strUS_EMAIL = txtEmail.Value.Trim();
        objUser.strUS_HOTEN = txtHoTen.Value.Trim();
        objUser.strUS_DIENTHOAI = txtDienThoai.Value.Trim();
        objUser.strUS_DIACHI = txtDiaChi.Value.Trim();
        if (rbtTrue.Checked)
        {
            objUser.blUS_NHANMAIL = true;
        }
        else
        {
            objUser.blUS_NHANMAIL = false;
        }

        string strAvarta_User = hdAvarta.Value;
        if (FupAvarta.HasFile)
        {
            string strOutImagesName;
            OsPortal.oFileHelper.UploadAnhResizeImage(FupAvarta, IMAGES_PATH, widthImages, out strOutImagesName, "", false);
            if (!string.IsNullOrEmpty(strOutImagesName))
                strAvarta_User = IMAGES_PATH + strOutImagesName;

        }
        objUser.strUS_AVATAR = strAvarta_User;

        return objUser;
    }

    //protected void btnCapNhat_Click(object sender, EventArgs e)
    //{
    //    string strResult = "";
    //    var strError = "";
    //    if (clsUsers.USER_ACT("U", xgetvalue(), out strError))
    //    {
    //        strResult = strError;
    //        if (strResult == "")
    //        {
    //            OsPortal.oHtmlHelper.AlertAndRederect(MSG_UPDATE_OK, "/users-manager/thong-tin-ca-nhan", this.Page);
    //        }
    //        else
    //        {
    //            OsPortal.oHtmlHelper.AlertAndRederect(strResult, "/users-manager/thong-tin-ca-nhan", this.Page);
    //        }
    //    }
    //}
}